package cn.laowen.user.service.impl;

import cn.laowen.basic.constants.BaseConstant;
import cn.laowen.basic.dto.RegisterDto;
import cn.laowen.basic.exception.BusinessException;
import cn.laowen.basic.util.MD5Utils;
import cn.laowen.basic.util.StrUtils;
import cn.laowen.user.domain.Logininfo;
import cn.laowen.user.domain.User;
import cn.laowen.user.mapper.LogininfoMapper;
import cn.laowen.user.mapper.UserMapper;
import cn.laowen.user.service.IUserService;
import cn.laowen.basic.service.impl.BasicServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laowen
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 手机号注册
     * @param registerDto
     */
    @Override
    public void phoneRegister(RegisterDto registerDto) {
        String phone = registerDto.getPhone();//手机号
        String smsCode = registerDto.getSmsCode();//短信验证码
        String password = registerDto.getPassword();//密码
        String passwordRepeat = registerDto.getPasswordRepeat();//确认密码
        //1.校验
        // 空校验
        //1.1密码和确认密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致,请重新输入!!!");
        }
        //2.短信验证码判断
        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        //2.1 拿不到验证码：说明验证码过期，提示重新获取
        if(smsCodeTmp == null){
            throw new BusinessException("验证码已失效，请重新获取!!!");
        }
        //2.2 拿到了验证码，判断是否一致
        if(!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
            // 2.2.1验证码不一致
            throw new BusinessException("请核对并输入正确的短信验证码!!!");
        }
        // 验证码一致
        //3.保存数据到：t_user + t_logininfo
        User user = registerDto2User(registerDto);//register对象转user对象
        Logininfo logininfo = user2Logininfo(user);//user对象转logininfo对象
        logininfoMapper.save(logininfo);//保存到 t_logininfo
        user.setLogininfoId(logininfo.getId());//给 user设置logininfo_id
        userMapper.save(user);//保存到 t_user
    }



    /**
     * user对象转logininfo对象
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);//使用工具类同名原则拷贝,把user中logininfo有的属性copy过去
        logininfo.setType(1);//前端用户注册  设置Type为1：
        return logininfo;
    }

    /**
     * register对象转user对象
     * @param registerDto
     * @return
     */
    private User registerDto2User(RegisterDto registerDto) {
        User user = new User();
        user.setUsername(registerDto.getPhone());//设置用户名
        user.setPhone(registerDto.getPhone());//设置手机号
        String salt = StrUtils.getComplexRandomString(32);//生成一个32位的盐值
        user.setSalt(salt);//设置盐值
        String md5Password = MD5Utils.encrypByMd5(registerDto.getPassword() + salt);//使用盐值加密密码
        user.setPassword(md5Password);//设置密码为加密后的密码
        return user;
    }
}
